import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

class ServerThread extends Thread {
    private Socket socket;
    public  Closer closer;

    public ServerThread(Socket socket) {
        this.socket = socket;
        closer = new Closer(socket);
    }

    @Override
    public void run() {
        ObjectInputStream ois;
        try {
            while (true) {
                ois = new ObjectInputStream(socket.getInputStream());
                Object o = ois.readObject();
                Message msgReceived;

                if(o instanceof Message) {
                    msgReceived = (Message)o;
                    ServerExamSystem ses = new ServerExamSystem(socket, msgReceived);
                    boolean readyDisconnect = ses.startProcess();
                    if (readyDisconnect) {
                        DataManager.outputDataToFile(Database.DATABASE_PATH);
                        closer.close();
                        break;
                    }
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
